<template>
  <div class="dashboard-container">
    <div class="dashboard-text">
      <el-form
        ref="employeeForm"
        :model="form"
        label-width="auto"
        style="max-width: 400px"
        :rules="rules"
        status-icon
      >
        <el-form-item label="账号：" prop="username">
          <el-input
            v-model="form.username"
            placeholder="请输入员工账号"
            clearable
          />
        </el-form-item>
        <el-form-item label="员工姓名：" prop="name">
          <el-input
            v-model="form.name"
            placeholder="请输入员工姓名"
            clearable
          />
        </el-form-item>
        <el-form-item label="手机号：" prop="phone">
          <el-input
            v-model="form.phone"
            placeholder="请输入手机号码"
            clearable
          />
        </el-form-item>
        <el-form-item label="性别：" prop="sex">
          <el-radio-group v-model="form.sex">
            <el-radio label="1">男</el-radio>
            <el-radio label="2">女</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="身份证号：" prop="idNumber">
          <el-input
            v-model="form.idNumber"
            placeholder="请输入身份证号码"
            clearable
          />
        </el-form-item>
      </el-form>
      <el-divider />
      <div class="buttons">
        <el-button type="primary" @click="cancelListMsg">取消</el-button>
        <el-button @click="saveListMsg">保存</el-button>
      </div>
    </div>
  </div>
</template>

<script>
import {
  checkUserName,
  checkName,
  checkPhone,
  validID
} from '@/utils/validateList'
import { addEmployee, queryEmployeeById } from '@/api/employeeManage'
export default {
  name: 'Employee',
  data() {
    return {
      form: {
        username: '',
        name: '',
        phone: '',
        sex: '1',
        idNumber: ''
      },
      rules: {
        username: [
          { required: true, validator: checkUserName, trigger: 'blur' }
          // { required: true, message: '请输入员工账号', trigger: 'blur' },
          // { min: 3, max: 20, message: '长度在3到20个字符', trigger: 'blur' }
        ],
        name: [
          { required: true, validator: checkName, trigger: 'blur' }
          // { required: true, message: '请输入员工姓名', trigger: 'blur' },
          // { min: 2, max: 10, message: '长度在2到10个字符', trigger: 'blur' }
        ],
        phone: [{ required: true, validator: checkPhone, trigger: 'blur' }],
        sex: [{ message: '请选择性别', trigger: 'change' }],
        idNumber: [{ required: true, validator: validID, trigger: 'blur' }]
      }
    }
  },
  computed: {
    id() {
      return this.$route.query.id
    },
    isEdit() {
      return this.$route.query.isEdit === '1'
    }
  },

  created() {
    if (this.isEdit) {
      queryEmployeeById(this.id).then((res) => {
        this.form = res.data
      })
    }
  },

  methods: {
    // 点击保存按钮
    async saveListMsg() {
      if (this.isEdit) {
        return
      } else {
        addEmployee(this.form).then((res) => {
          this.$message.success('保存成功')
          this.$router.push({ name: 'employeeManage' })
        })
      }
    },
    // 点击取消按钮
    cancelListMsg() {
      // 跳转到员工管理页面
      this.$router.push({ name: 'employeeManage' })
    }
  }
}
</script>

<style lang="scss" scoped>
.dashboard {
  &-container {
    margin: 30px;
  }
  &-text {
    font-size: 30px;
    line-height: 36px;

    .buttons {
      text-align: center;
    }
  }
}
</style>
